/* global Buffer process */
const path = require('path')
const fs = require('fs')
const moment = require('moment')
// 根据时间戳生成文件名
const generationTimestampFileName = () => {
  return Date.now()
}
// 随机生成文件名
const generateRandomString = length => {
  let result = ''
  const characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  const charactersLength = characters.length
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength))
  }
  return result
}

// 判断指定目录是否存在, 不存在则创建
const notExitCreate = dir => {
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true })
  }
}

// 将 base64 字符串转化为图片并存储在文件目录中
const base64ToImage = (name, base64String) => {
  console.log('Date.now()', name, Date.now())
  // 去除 Base64 URL 的头部（如'data:image/png;base64,'）以获取纯粹的 Base64 编码数据
  const base64Data = base64String.replace(/^data:([A-Za-z-+/]+);base64,/, '')
  // 将 Base64 编码的字符串转换成 Buffer 对象
  const dataBuffer = Buffer.from(base64Data, 'base64')
  // 创建目录
  const mimeType = base64String.match(/^data:(image\/\w+);base64,/)[1]
  const mimeParts = mimeType.split('/')[1]

  // const outputPath = `/public/uploads/image`
  const dateFolder = moment().format("YYYY-MM-DD")
  let outputPath = ''
  if (name !== '') {
    outputPath = `/public/uploads/${name}/${dateFolder}`
  } else {
    outputPath = `/public/uploads/${dateFolder}`
  }
  notExitCreate(path.join(process.cwd(), outputPath))
  // 方法1：使用  fs.writeFileSync 方法将 Buffer 对象写入到指定的文件路径
  // const filePath = `${outputPath}/${generateRandomString(32)}.${mimeParts}`
  // 方法2：使用  fs.writeFileSync 方法将 Buffer 对象写入到指定的文件路径,文件名为当前时间戳
  const filePath = `${outputPath}/${generationTimestampFileName()}.${mimeParts}`
  fs.writeFileSync(path.join(process.cwd(), filePath), dataBuffer)
  return filePath
}

// 判断指定路径是否存在, 不存在则创建
module.exports = {
  generateRandomString,
  notExitCreate,
  base64ToImage
}
